import * as types from '../actions/types'
import createReducer from './createReducer'
import isEmpty from 'lodash/isEmpty'

const initial = {
	isAuthed: false,
	showLogin: false,
	user: {
		username: '',
		password: ''
	}
}

export const auth = createReducer(initial,{
	[types.SET_CURRENT_USER](state, action){
		const user = action.user
		return Object.assign({} , state, {
			isAuthed: !isEmpty(user),
			user: user
		})
	},
	[types.SET_LOGINPAGE_VISIBLE](state, action) {
		return Object.assign({}, state, {showLogin: action.showLogin})
	},
	[types.SET_USER_NAME](state, action) {
		let user = state.user;
		user.username = action.username
		return Object.assign({}, state, {user})
	},
	[types.SET_PASSWORD](state, action) {
		let user = state.user;
		user.password = action.password
		return Object.assign({}, state, {user})
	}
})
